package com.imay.live.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.utils.GlideUtil;

/**
 * Created by liangzhenxiong on 2017/04/07 0007.
 */

public class GridLinearLayout extends LinearLayout {
    int space = 0;
    ImageView iv_back0, iv_back1;
    TextView tv_location0, tv_location1;
    TextView tv_countuser0, tv_countuser1;

    public GridLinearLayout(Context context) {
        super(context);
        init(context);
    }

    public GridLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GridLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public GridLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    void init(Context context) {
        space = (int) context.getResources().getDimension(R.dimen.hot_live_head_space);
        View headview0 = LayoutInflater.from(context).inflate(R.layout.item_discoverylive_head, this, false);
        iv_back0 = (ImageView) headview0.findViewById(R.id.iv_back);
        tv_location0 = (TextView) headview0.findViewById(R.id.tv_location);
        tv_countuser0 = (TextView) headview0.findViewById(R.id.tv_countuser);
        addView(headview0);
        View view = new View(context);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.width = space;
        view.setBackgroundColor(Color.WHITE);
        addView(view, layoutParams);
        View headview1 = LayoutInflater.from(context).inflate(R.layout.item_discoverylive_head, this, false);
        iv_back1 = (ImageView) headview1.findViewById(R.id.iv_back);
        tv_location1 = (TextView) headview1.findViewById(R.id.tv_location);
        tv_countuser1 = (TextView) headview1.findViewById(R.id.tv_countuser);
        addView(headview1);
    }

    public void setImgs(String img, String imgs) {
        if (!TextUtils.isEmpty(img)) {
            GlideUtil.loadImage(iv_back0,img,R.mipmap.placeholder);
        }
        if (!TextUtils.isEmpty(imgs)) {
            GlideUtil.loadImage(iv_back1,imgs,R.mipmap.placeholder);
        }
    }

    public void setLocation(String location, String locations) {
        tv_location0.setText(location);
        tv_location1.setText(locations);
    }

    public void setCountUser(int countuser, int countusers) {
        tv_countuser0.setText(String.valueOf(countuser));
        tv_countuser1.setText(String.valueOf(countusers));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childw = (getMeasuredWidth() - space) >> 1;
        int maxh = 0;

        for (int i = 0; i < getChildCount(); i++) {
            if (i == 1) {
                continue;
            }
            getChildAt(i).measure(MeasureSpec.makeMeasureSpec(childw, MeasureSpec.EXACTLY), heightMeasureSpec);
            maxh = getChildAt(i).getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), maxh);
    }
}
